﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Collections.ObjectModel;

namespace JEng_Mark_1.Managers
{
    [XmlType("configuration")]
    public sealed class JConfigurationManager
    {
        public static JConfigurationManager Instance { get; set; }
        public static string ConfigurationPath { get; set; }

        static JConfigurationManager()
        {
            string configPath = "Content/Settings/config.xml";
            if (Instance == null)
            {
                if (!File.Exists(configPath))
                {
                    throw new FileNotFoundException();
                }

                using (StreamReader reader = new StreamReader(configPath))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(JConfigurationManager));
                    Instance = (JConfigurationManager)serializer.Deserialize(reader);
                }
            }
        }

        [XmlArray("managers"), XmlArrayItem("manager")]
        public Managers Managers { get; set; }

        [XmlArray("components"), XmlArrayItem("component")]
        public Components Components { get; set; }
    }

    public sealed class Components : KeyedCollection<string,Component>
    {
        protected override string GetKeyForItem(Component item)
        {
            return item.Value;
        }
    }

    public sealed class Managers : KeyedCollection<string,Manager>
    {
        protected override string GetKeyForItem(Manager item)
        {
            return item.Value;
        }
    }

    [XmlType("component")]
    public sealed class Component
    {
        [XmlAttribute("value")]
        public string Value { get; set; }
    }

    [XmlType("manager")]
    public sealed class Manager
    {
        [XmlAttribute("value")]
        public string Value { get; set; }
    }
}
